<?php

defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$uniacid = $_SESSION['uniacid'];
$mid = $_SESSION['uid'];
$basic=$this->plugin_setting('basic');
$return['advs']=$basic['image'];
$return['basic']=$basic;
if($_RQ['op']=='main'){
	/*******main*******/
	//商品详情
	$id = intval($_RQ['id']);
	if (empty($id)){
			exi( '活动不存在！','error');
		}
	$group=pdo_get('jy_aligroup_groups',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$goods=pdo_get('jy_weishop_goods',array('id'=>$group['goodsid'],'uniacid'=>$_SESSION['uniacid']));
	if(empty($goods)) {		
		exi( '你访问的商品不存在或已删除','error');
	}
	$goods['fightnum'] = pdo_count('jy_aligroup_groups_order',array('goodsid' => $goods['id'], 'uniacid'=>$_SESSION['uniacid'],'is_team' => 1, 'status >' => 0),'id');
	$goods['fightnum'] = $group['teamnum'] + $goods['fightnum'];
	if(!$group['feetype'] && $group['agio']!=0){
		$goods['groupsprice'] = $group['agio']*$goods['price']/100;
	}elseif($group['feetype'] && $group['price']!=0){
		$goods['groupsprice']=$group['price'];
	}else{
		$goods['groupsprice'] =$goods['price'];
	}
	//商品多图
	$goods['images']=$goods['images']?json_decode($goods['images'],true):array(0=>$goods['image']);
	$return['list']=array();
	$group['endstatus']=$group['starttime']<time() ? $group['endtime']>time()? 1 : 2 : 0;
	$group['statustitle']=$group['starttime']<time() ? $group['endtime']>time()? '去拼团' : '已结束' : '未开始';
	$return['goods']=$goods;
	$return['group']=$group;
	$return['sharedata']=array(
			'title'=>$goods['title']?$goods['title'].'-拼团':'拼团详情',
			'desc'=>$goods['subtitle']?$goods['subtitle']:'拼团详情-在线拼团商城！',
			'link'=>custom_url('jy_aligroup/goods//id='.$id.'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>tomedia($goods['image'])
	);
	set_html('title',$goods['title']?:'拼团详情');
	exi($return);
}
if($_RQ['op']=='getteams'){
	$groupid=intval($_RQ['groupid']);
	$group=pdo_get('jy_aligroup_groups',array('id'=>$groupid,'uniacid'=>$_SESSION['uniacid']));
	$pindex = max(1, intval($_RQ['page']));
	$psize =6;
	$return['params']=$params=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'groupid'=>$groupid);
	$status=intval($_RQ['status']);
	$params['status']=$status;
	$return['params']['status']=$status;
	$num=intval($_RQ['num']);
	if($_RQ['keyword']){
		$params['title like']='%'.$_RQ['keyword'].'%';
		$return['params']['keyword']=$_RQ['keyword'];
	}
	
	if($num){
		$psize = $num;
	}
	$times=array();
	$group['timeout']=$group['timeout']?:24;
	$list=pdo_getslice('jy_aligroup_groups_team',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach ($list as &$team) {
			//查询对应拼团还需人数
			$num1 = pdo_count('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$team['id'],'status'=>1));
			$team['num'] = $group['groupnum'] - $num1;
			//查询团长信息
			$team['header']=cfc('core')->member_show($team['headsid']);
			//查询团长订单
			$order=pdo_get('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$team['headsid'],'headsid'=>$team['headsid'],'groupid'=>$id));
			//查询团长地址
			$team['address']=pdo_get('core_address',array('id'=>$order['addressid']));
			$times[$team['id']]=$team['times']=model('jy_weishop/model')->date($team['createtime']+$group['timeout']*3600-TIMESTAMP);
		}
	}
	$return['times']=$times;
	$return['list']=$list;
	exi($return);
}
